<?php
namespace app\index\controller;
// use think\View;
use app\common\controller\CommonFun;
use app\common\controller\LogsCm;
use think\Controller;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\Session;
use app\common\controller\UserCm;
use think\Validate;
use app\index\model\Pass;

class login extends Controller{

    /**渲染模板
     * @return mixed
     * @author LeeBW
     * @date 2020-12-16 16:08
     */
    public function index(){
		// $view = new View;
		return $this->fetch();
	}

    /**检验验证码
     * @author LeeBW
     * @date 2020-12-16 16:08
     */
    public function checkcode()
	{
		$code=input('post.user_vertify');
		if(!captcha_check($code))
		{
		 echo "验证码错误！";
		} else {
		 echo "验证通过！";
		}
	}

    /**登录
     * @param string $user_name
     * @param string $user_passwd
     * @throws \think\exception\DbException
     * @author LeeBW
     * @date 2020-12-16 16:08
     */
//    public function login($user_name='', $user_passwd=''){
//        $sid = CommonFun::GetRandomString(32, 1);
//		$user=Pass::get([
//				'username' => $user_name,
//				'passward' => $user_passwd
//			]);
//		Session::set('name',$user_name);
//		$code=input('post.user_vertify');
//		if(!captcha_check($code))
//		{
//		 	return $this->error('验证码错误');
//		} else {
//			 if($user){
//				return $this->success('登录成功！','/index');
//			}else{
//				return $this->error('用户名或密码不正确');
//			}
//		}
//	}



    /**
     * 登陆，密码校验
     * @return array 返回sid  或 false
     * @throws Exception
     * @throws PDOException
     */
    public function Login(){
        //参数
        $username = $this->request->param('username', '');
        $password = $this->request->param('password', '');
        $code = $this->request->param('verify_num', '');
        if(!captcha_check($code))
        {
            $data['status'] = 1;
            $data['msg'] = '验证码错误';
            return $data;

        } else {
            $user =new UserCm();
            $password_db = $user->GetField($username, 'password');
            if($password != $password_db){
//                return $this->error('用户名或密码不正确');
                $data['status'] = 1;
                $data['msg'] = '用户名或密码不正确';
                LogsCm::WriteAdmLog($username,'用户登录：'.$data['msg']);
                return $data;
            }

            $level = $user->GetField($username, 'level');
            Session::set('name',$username);
            Session::set('level',$level);
            $crt_time = CommonFun::GetTime();
            $sid = CommonFun::GetRandomString(32, 1);
            $req_ip = BaseController::get_ip();
            $data = ['sid'=>$sid,'login_time'=>$crt_time,'req_ip'=>$req_ip];
            $cdn['username'] = $username;
            Db::name('user')->where($cdn)->update($data);

            $data['status'] = 0;
            $data['msg'] = '登录成功';
            LogsCm::WriteAdmLog($username,'用户登录：'.$data['msg']);
            return $data;
        }
    }

    /**
     * 登出
     * @return mixed
     * @throws Exception
     * @throws PDOException
     */
    public function DoLogout()
    {
        //参数
        $username = $this->request->param('user');
        //更新
        $obj_manager = new UserCm();
        $obj_manager->Logout($username);

        $data['status'] = 0;
        $data['msg'] = '注销成功';
        $data['addr'] = '注销成功';
        LogsCm::WriteAdmLog($username,'用户注销：'.$data['msg']);
        Session::clear();
        return $data;


    }
}